# -*- coding: utf-8 -*-
"""
Created on Tue Aug 13 18:29:24 2019

7.6节 多元逻辑回归

@author: haodong
"""

import numpy as np
import statsmodels.api as sm
import pandas as pd

df=pd.read_csv('multi_logit.csv',header=None)

df.columns = ["y", "x1", "x2"]

features = ["x1", "x2"]
labels = ["y"]

#一对多分类
model = LogisticRegression(multi_class='ovr', solver='sag',
            max_iter=1000, random_state=42)
model.fit(df[features], df[labels])
model.coef_
model.intercept_



#多元逻辑回归
model = LogisticRegression(multi_class='multinomial', solver='sag',
            max_iter=1000, random_state=42)
model.fit(df[features], df[labels])
model.coef_
model.intercept_

#分类概率
prob = model.predict_proba(df[features])
prob

#预测与混淆矩阵
pred = model.predict(df[features])
print(metrics.classification_report(df['y'],pred))
metrics.confusion_matrix(df['y'],pred)
